/*
 *  This file is part of AtlasMapper server and clients.
 *
 *  Copyright (C) 2012 Australian Institute of Marine Science
 *
 *  Contact: Gael Lafond <g.lafond@aims.org.au>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package au.gov.aims.metadatagenerator.wkt;

import java.text.DecimalFormat;
import java.util.List;

public abstract class WktObject {
	protected final DecimalFormat decimalFormat = new DecimalFormat("#.#######");
	protected final DecimalFormat bboxDecimalFormat = new DecimalFormat("#.###");

	private Double northBound = null;
	private Double eastBound = null;
	private Double southBound = null;
	private Double westBound = null;

	public abstract boolean isEmpty();

	public Double getNorthBound() { return this.northBound; }
	public String getFormattedNorthBound() { return this.bboxDecimalFormat.format(this.northBound); }
	public void setNorthBound(Double northBound) { this.northBound = northBound; }

	public Double getEastBound() { return this.eastBound; }
	public String getFormattedEastBound() { return this.bboxDecimalFormat.format(this.eastBound); }
	public void setEastBound(Double eastBound) { this.eastBound = eastBound; }

	public Double getSouthBound() { return this.southBound; }
	public String getFormattedSouthBound() { return this.bboxDecimalFormat.format(this.southBound); }
	public void setSouthBound(Double southBound) { this.southBound = southBound; }

	public Double getWestBound() { return this.westBound; }
	public String getFormattedWestBound() { return this.bboxDecimalFormat.format(this.westBound); }
	public void setWestBound(Double westBound) { this.westBound = westBound; }

	public void updateNorthSouthBound(Double value) {
		if (value != null) {
			if (this.northBound == null) {
				this.northBound = value;
			} else if (value > this.northBound) {
				this.northBound = value;
			}
			if (this.southBound == null) {
				this.southBound = value;
			} else if (value < this.southBound) {
				this.southBound = value;
			}
		}
	}

	public void updateEastWestBound(Double value) {
		if (value != null) {
			if (this.westBound == null) {
				this.westBound = value;
			} else if (value > this.westBound) {
				this.westBound = value;
			}
			if (this.eastBound == null) {
				this.eastBound = value;
			} else if (value < this.eastBound) {
				this.eastBound = value;
			}
		}
	}

	public void updateBound(List<? extends WktObject> objects) {
		for (WktObject obj : objects) {
			this.updateBound(obj);
		}
	}

	public void updateBound(WktObject object) {
		this.updateNorthSouthBound(object.getNorthBound());
		this.updateNorthSouthBound(object.getSouthBound());

		this.updateEastWestBound(object.getEastBound());
		this.updateEastWestBound(object.getWestBound());
	}

	/**
	 * This method return the WKT String.
	 * @return
	 */
	@Override
	public abstract String toString();
}
